package org.lk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lk.reggie.common.BaseContext;
import org.lk.reggie.common.CustomException;
import org.lk.reggie.entity.*;
import org.lk.reggie.mapper.OrderMapper;
import org.lk.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrdersService
{

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    @Transactional
    public void mySubmit(Orders orders) {
        // 获取当前用户 id
        Long userId = BaseContext.getCurrentId();
        // 获取当前用户的购物车信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart :: getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        // 向订单表插入数据
        if (shoppingCartList == null || shoppingCartList.size() == 0)
        {
            throw new CustomException("购物车为空");
        }
        // 获取用户信息
        User user = userService.getById(userId);
        // 获取地址信息
        Long addressBookId = orders.getAddressBookId();
        AddressBook address = addressBookService.getById(addressBookId);
        if (address == null)
        {
            throw new CustomException("地址有误");
        }
        AtomicInteger atomicInteger = new AtomicInteger(0);
        long id = IdWorker.getId();
        // 遍历购物车数据，获取订单总金额,并整理得出订单明细
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shop:
             shoppingCartList)
        {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(id);
            orderDetail.setNumber(shop.getNumber());
            orderDetail.setDishFlavor(shop.getDishFlavor());
            orderDetail.setDishId(shop.getDishId());
            orderDetail.setSetmealId(shop.getSetmealId());
            orderDetail.setName(shop.getName());
            orderDetail.setImage(shop.getImage());
            orderDetail.setAmount(shop.getAmount());
            atomicInteger.addAndGet(shop.getAmount().multiply(new BigDecimal(shop.getNumber())).intValue());
            orderDetailList.add(orderDetail);
        }

        orders.setNumber(String.valueOf(id));
        orders.setUserId(userId);
        orders.setId(id);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setUserName(user.getName());
        orders.setConsignee(address.getConsignee());
        orders.setPhone(user.getPhone());
        orders.setAddress("虚假的地址");
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        // 向订单明细表插入数据
        orderDetailService.saveBatch(orderDetailList);
        // 清空购物车
        shoppingCartService.remove(queryWrapper);
    }
}
